package com.serene.authority.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serene.authority.admin.entity.MyRoleUser;
import com.serene.authority.admin.mapper.RoleUserMapper;
import com.serene.authority.admin.service.RoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author serence
 * @date 2021/5/8 20:49
 */

@Service
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, MyRoleUser> implements RoleUserService {

    @Autowired
    private RoleUserMapper roleUserMapper;


    /**
     * 根据用户id删除用户角色
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public Integer deleteRoleUserByUserId(Integer userId) {
        QueryWrapper<MyRoleUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return roleUserMapper.delete(wrapper);
    }

    /**
     * 根据用户id查询用户角色
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public MyRoleUser queryRoleByUserByUserId(Integer userId) {
        QueryWrapper<MyRoleUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return roleUserMapper.selectOne(wrapper);
    }

    /**
     * 根据角色id查询用户角色
     *
     * @param roleId 角色id
     * @return
     */
    @Override
    public List<MyRoleUser> queryRoleByUserByRoleId(Integer roleId) {
        QueryWrapper<MyRoleUser> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        return roleUserMapper.selectList(wrapper);
    }


    /**
     * 根据用户id查询用户角色信息
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public List<MyRoleUser> queryRoleByUserByUserIdList(Integer userId) {
        QueryWrapper<MyRoleUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return roleUserMapper.selectList(wrapper);
    }

}
